﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;

namespace 视频流媒体管理
{
    /// <summary>
    /// api_v1 的摘要说明
    /// </summary>
    public class api_v1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            try
            {
                if (Code.摄像机管理.ONE.token == "")
                    throw new Exception("服务器未登录");

                string method = context.Request.QueryString["method"];
                StreamReader sr = new StreamReader(context.Request.GetBufferlessInputStream());
                string response = sr.ReadToEnd();
                sr.Close();

                string json = "";
                //if (method == "login")
                //{
                //    this.登录(context, response);
                //}
                //else if (method == "logout")
                //{
                //    this.退出登录(context, response);
                //}
                if (method == "live_list")
                {
                    json = this.取得直播列表();
                }
                else if (method == "live_get")
                {
                    json = this.取得直播列表_单个(response);
                }
                else if (method == "turn_actived")
                {
                    json = this.打开关闭直播(response);
                }
                else if (method == "live_save")
                {
                    json = this.新增直播(response);
                }
                else if (method == "live_remove")
                {
                    json = this.删除直播(response);
                }
                else if (method == "keeplive")
                {
                    json = this.保活摄像机(response);
                }
                else if (method == "xls_live")
                {
                    json = this.新增xls直播(response);
                }
                else
                {
                    throw new Exception("未知命令：" + method);
                }
                context.Response.Write(json);
            }
            catch (Exception ex)
            {
                JObject ret = new JObject();
                ret["错误信息"] = ex.Message;
                context.Response.Write(ret.ToString());
            }
        }

        //private void 登录(HttpContext context, string postdat)
        //{
        //    JObject p = JObject.Parse(postdat);
        //    string username = p["username"].ToString();
        //    string password = p["password"].ToString();

        //    string pwd = Sea.Common.Encrypt.MD5.生成MD5(password).ToLower(); //口令用md5加密，小写
        //    string url = Global.流媒体主机名 + "/login";            //https://blog.csdn.net/qq_51860400/article/details/132455026

        //    Dictionary<string, string> dict = new Dictionary<string, string>();
        //    dict["username"] = username;
        //    dict["password"] = pwd;
        //    dict["onlytoken"] = "true";

        //    视频流媒体管理.App_Code.HttpPost hp = new App_Code.HttpPost();
        //    string cons = hp.组合FormData(dict, out string contType);
        //    string str = hp.PostData(url, contType, cons, new Dictionary<string, string>());
        //    //{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzAyNjcyMDksInB3IjoiMjEyMzJmMjk3YTU3YTVhNzQzODk0YTBlNGE4MDFmYzMiLCJ0bSI6MTczMDE4MDgwOSwidW4iOiJhZG1pbiJ9.a0ok1e7ohUbd5_2mdDOrsYJ3owelMxA4MKVfZOye_Bg"}

        //    if (str.Contains("token") == true)
        //    {
        //        context.Response.Write(str); //登录成功，本身就是json格式
        //    }
        //    else
        //    {
        //        JObject ob = new JObject();
        //        ob["错误信息"] = str;
        //        context.Response.Write(ob.ToString());
        //    }
        //}

        //private void 退出登录(HttpContext context, string postdat)
        //{
        //    JObject p = JObject.Parse(postdat);
        //    string token = p["token"].ToString();

        //    string url = Global.流媒体主机名 + "/logout?token=" + token;
        //    视频流媒体管理.App_Code.HttpPost hp = new App_Code.HttpPost();
        //    string str = hp.GetData(url, "", new Dictionary<string, string>());
        //    context.Response.Write(str); //登录成功，本身就是json格式
        //}

        private string 取得直播列表()
        {
            //JObject p = JObject.Parse(postdat);
            //string token = p["token"].ToString();
            DataTable dt = Code.摄像机管理.ONE.取得直播列表();

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(dt);
            return json;//  context.Response.Write(json); //登录成功，本身就是json格式
        }

        private string 取得直播列表_单个(string postdat)
        {
            JObject p = JObject.Parse(postdat);
            string id = p["id"].ToString();
            DataTable dt = Code.摄像机管理.ONE.取得直播列表_单个(id);

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(dt);
            return json; // context.Response.Write(json); //登录成功，本身就是json格式
        }

        private string 打开关闭直播(string postdat)
        {
            string str = Code.摄像机管理.ONE.打开关闭直播(postdat);
            return str; // context.Response.Write(str); //登录成功，本身就是json格式
        }

        private string 新增直播(string postdat)
        {
            string str = Code.摄像机管理.ONE.新增直播(postdat);
            return str; // context.Response.Write(str); //登录成功，本身就是json格式
        }

        private string 新增xls直播(string postdat)
        {
            JObject p = JObject.Parse(postdat);
            string id = p["id"].ToString();
            string str = Code.摄像机管理.ONE.新增xls直播(id);
            return str; // context.Response.Write(str); //登录成功，本身就是json格式
        }

        private string 删除直播(string postdat)
        {
            string str = Code.摄像机管理.ONE.删除直播(postdat);
            return str; // context.Response.Write(str); //登录成功，本身就是json格式
        }

        private string 保活摄像机(string postdat)
        {
            JObject p = JObject.Parse(postdat);
            string id = p["id"].ToString();
            Code.摄像机管理.ONE.摄像机保活(id);
            p["keeplive"] = "ok";
            return p.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}